package com.wjerp.tplus.server.api.controller.advice;

import com.wjerp.tplus.server.common.constant.ErrorDetail;
import com.wjerp.tplus.server.common.exception.UnAuthException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

	@Override
	protected ResponseEntity<Object> handleHttpMessageNotReadable(
            HttpMessageNotReadableException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
		BaseResponseData data = new BaseResponseData(ErrorDetail.REQ_BADLY_SET_ERROR_GENERAL.getErrorCode()
				, ErrorDetail.REQ_BADLY_SET_ERROR_GENERAL.getErrorMsg(), null);
		return new ResponseEntity<Object>(data, status);
	}	
	
//	@ExceptionHandler(value = {MessagingException.class})
//	protected ResponseEntity<Object> handleMessagingException(
//			MessagingException ex, WebRequest request){
//		BaseResponseData data = new BaseResponseData(ErrorDetail.INTERNAL_SYSTEM_ERROR.getErrorCode(),
//				ErrorDetail.INTERNAL_SYSTEM_ERROR.getErrorMsg(), null);
//		return new ResponseEntity<Object>(data, HttpStatus.INTERNAL_SERVER_ERROR);
//	}

	@ExceptionHandler(value = {UnAuthException.class})
	public ResponseEntity<Object> handleServiceException(Exception ex, WebRequest request) {
		BaseResponseData data = new BaseResponseData(ErrorDetail.AUTH_INVALID_TOKEN.getErrorCode(),
				ErrorDetail.AUTH_INVALID_TOKEN.getErrorMsg(), null);
		return new ResponseEntity<Object>(data, HttpStatus.UNAUTHORIZED);
	}
}
